package com.ruoyi.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VersionsUpgrade;
import com.ruoyi.system.service.IVersionsUpgradeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【版本管理】Controller
 * 
 * @author ruoyi
 * @date 2023-08-30
 */
@Api("版本管理")
@RestController
@RequestMapping("/system/upgrade")
public class VersionsUpgradeController extends BaseController
{
    @Autowired
    private IVersionsUpgradeService versionsUpgradeService;

    /**
     * 查询版本管理列表
     */
    @ApiOperation("查询版本管理列表")
    @PreAuthorize("@ss.hasPermi('system:upgrade:list')")
    @GetMapping("/list")
    public TableDataInfo list(VersionsUpgrade versionsUpgrade)
    {
        startPage();
        List<VersionsUpgrade> list = versionsUpgradeService.selectVersionsUpgradeList(versionsUpgrade);
        return getDataTable(list);
    }

    /**
     * 导出版本管理列表
     */
    @ApiOperation("导出版本管理列表")
    @PreAuthorize("@ss.hasPermi('system:upgrade:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VersionsUpgrade versionsUpgrade)
    {
        List<VersionsUpgrade> list = versionsUpgradeService.selectVersionsUpgradeList(versionsUpgrade);
        ExcelUtil<VersionsUpgrade> util = new ExcelUtil<VersionsUpgrade>(VersionsUpgrade.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取版本管理详细信息
     */
    @ApiOperation("获取版本管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:upgrade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(versionsUpgradeService.selectVersionsUpgradeById(id));
    }

    /**
     * 新增版本管理
     */
    @ApiOperation("新增版本管理")
    @PreAuthorize("@ss.hasPermi('system:upgrade:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VersionsUpgrade versionsUpgrade) throws ParseException {
        List<VersionsUpgrade> versionList= versionsUpgradeService.findVersion();
        List<String> versionNum=new ArrayList<>();
        for (VersionsUpgrade upgrade : versionList) {
            String verNumber = upgrade.getVerNumber();
            versionNum.add(verNumber);
        }
        if (!versionNum.contains(versionsUpgrade.getVerNumber())){
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(System.currentTimeMillis());

            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=dateFormat.parse(format);

            versionsUpgrade.setUpTime(date);

            return toAjax(versionsUpgradeService.insertVersionsUpgrade(versionsUpgrade));

        }else {
            return AjaxResult.error("版本号重复，请重新输入");
        }

    }

    /**
     * 修改【版本管理】
     */
    @ApiOperation("修改版本管理")
    @PreAuthorize("@ss.hasPermi('system:upgrade:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VersionsUpgrade versionsUpgrade)
    {
        return toAjax(versionsUpgradeService.updateVersionsUpgrade(versionsUpgrade));
    }

    /**
     * 删除【版本管理】
     */
    @ApiOperation("删除版本管理")
    @PreAuthorize("@ss.hasPermi('system:upgrade:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(versionsUpgradeService.deleteVersionsUpgradeByIds(ids));
    }
}
